//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    is_authorize_location: '',
    hidden: false,
    cityArr: {},
    positioName:'',
    nearLocationArr:[],
    type: ''
  },

  onLoad: function (options) {
    var type = options.type
    var that = this;
    wx.getStorage({
      key: 'near-location',
      success: function(res) {
        that.setData({
          nearLocationArr: res.data,
          type: type
        })
      },
    })
  },
  onShow:function(){
    this.authSetting(0);
    this.setData({
      city: app.globalData.city,
      is_authorize_location: app.globalData.is_authorize_location,
      hidden: false,
      positioName: app.globalData.positioName
    })
  },
  //跳转选择城市页面
  chosseCity: function() {
    wx.navigateTo({
      url: '../city/city',
    })
  },
  focus:function(){
    this.setData({
      hidden: true
    })
  },
  blur: function () {
    this.setData({
      hidden: false
    })
  },
  searchCity: function(e) {
    var that = this;
    var city = this.data.city;
    if (city == ''){
      wx.showModal({
        content: '请先选择城市',
        confirmText: '确定',
        success: function(res){
          if (res.confirm == true){
            wx.navigateTo({
              url: '../city/city',
            })
          }
        }
      })
      return false;
    }
    var keyword = e.detail.value;
    if (keyword == ''){
      return false;
    }
    app.wxPost(app.globalData.host + '/info/searchLocation', { from: city, keyword: keyword }).then(res=>{
      that.setData({
        cityArr: res
      })
    });
  },
  chooseCity:function(e){
    var type = this.data.type;
    var lat = e.currentTarget.dataset.lat;
    var lng = e.currentTarget.dataset.lng;
    var title = e.currentTarget.dataset.title;
    if(type == 1){
      app.globalData.address_latitude = lat;
      app.globalData.address_longitude = lng;
      app.globalData.address_positioName = title;
      wx.navigateBack({
        url: '../../mine/address/address',
      })
    } else {
      app.globalData.latitude = lat;
      app.globalData.longitude = lng;
      app.globalData.positioName = title;
      wx.switchTab({
        url: '../index-online'
      })
    }
  },
  reposition: function(){
    app.globalData.latitude = '';
    app.globalData.longitude = '';
    app.globalData.positioName = '';
    this.authSetting(1);
  },
  authSetting: function (type) {
    var that = this;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          app.globalData.is_authorize_location = 0;
          that.setData({
            is_authorize_location: 0
          })
          wx.showModal({
            content: '佳品小栈需要您的地理位置',
            confirmText: '去开启',
            success: function (res) {
              if (res.confirm == true) {
                wx.openSetting({
                  success: function (res) {
                    if (res.authSetting['scope.userLocation'] == true) {
                      wx.switchTab({
                        url: '../index-online'
                      })
                    }
                  }
                })
              }
            },
          })
        } else if (res.authSetting['scope.userLocation'] == true && type == 1) {
          wx.switchTab({
            url: '../index-online'
          })
        }
      }
    }) 
  }
})